/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.helper;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Factory that can create and delete a {@link org.eclipse.uml2.uml.Class} in the context of
 * a composite diagram.
 */
public class NodeInstanceSpecificationClassHelper extends com.cea.papyrus.diagram.classdiagram.helper.NodeInstanceSpecificationHelper {
	
	///////////////////////////////////////////////////////////////////////////
    // Graphical Management
    ///////////////////////////////////////////////////////////////////////////

	/* (non-Javadoc)
	 * @see com.cea.papyrus.diagram.classdiagram.factory.NodeClassFactory#createGraphElement(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.GraphElement, org.eclipse.draw2d.geometry.Point, org.eclipse.draw2d.geometry.Dimension)
	 */
	/**
	 * 
	 * 
	 * @param umlChild 
	 * @param size 
	 * @param location 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public GraphNode createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size){
		Debug.debug(this,"Call NodeClassFactory.createGraphNode()", channel); //$NON-NLS-1$
		NodeHelper nh= new NodeHelper();
		// creates a simple graph node (use createGrpahElement) specific to NodeFactory, not NodeClassFacotry from class diagram
		GraphNode graphNode=nh.createGraphElement(umlChild, null, location, size);
		
		//the ge parent is connected in last because an event may be sent at this moment and trigger a refresh.
		parent.getContained().add(graphNode);
		return graphNode;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.diagram.classdiagram.factory.NodeClassFactory#createGraphNode(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	/**
	 * 
	 * 
	 * @param umlElement 
	 * @param diagram 
	 * 
	 * @return 
	 */
	@Override
	public ArrayList createGraphNode(Element umlElement, Diagram diagram) {

		Debug.debug(this,"Call NodeClassFactory.createGraphNode()", channel);
		ArrayList createdNode =super.createGraphNode(umlElement, diagram);

		Iterator it =createdNode.iterator();
		while(it.hasNext()){
			//current node
			GraphNode currentNode=(GraphNode)it.next();
			currentNode.setSize(new Dimension(100,50));
		}
		return createdNode;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.diagram.classdiagram.factory.NodeClassFactory#canCreateGraphElement(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		Element parentElt = null;
		if(parent instanceof Diagram) {
			// should check if parent semantic model element is a right parent
			parentElt= ((Uml1SemanticModelBridge)((Diagram)parent).getOwner()).getElement();
		} else if (parent.getSemanticModel()!=null){
			parentElt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();
		}
		
		if(parentElt!=null){
			return canCreateUMLElement(parentElt);
		}
		
		return false;
	}


}